package org.lboot.jpa.converter;

import cn.hutool.core.lang.Validator;
import cn.hutool.json.JSONUtil;
import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author kindear
 * JPA 对象数组转化
 */
@Converter
public class JpaListObjectConverter implements AttributeConverter<List<Object>, String> {
    @Override
    public String convertToDatabaseColumn(List<Object> objects) {
        if (Validator.isEmpty(objects)) {
            objects = new ArrayList<>();
            return JSONUtil.toJsonStr(objects);
        } else {
            return JSONUtil.toJsonStr(objects);
        }
    }

    @Override
    public List<Object> convertToEntityAttribute(String s) {
        if (Validator.isEmpty(s)){
            return new ArrayList<>();
        }else {
            return JSONUtil.toList(s,Object.class);
        }
    }
}
